package com.cinbo.controller;


import com.cinbo.dto.UsersDTO;
import com.cinbo.iface.MyDBOperator;
import com.cinbo.mybatisplus.UserInfo;
import com.cinbo.mybatisplus.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;


import java.util.Date;
import java.util.List;

@RestController
public class WebMvcController {

    @Autowired
    private MyDBOperator myDBOperator;

    @Autowired
    private UserMapper userMapper;


    @GetMapping("/hello")
    public String testurl(){
        UsersDTO usersDTO = new UsersDTO();
            usersDTO.setUsername("testname");
            usersDTO.setPassword("123");
            usersDTO.setLogindate(new Date());
            usersDTO.setMyvalue("我的值");
            myDBOperator.insert(usersDTO);


        return "this is test web";
    }

    @GetMapping("/getrow")
    public String testgetrow(){
        UserInfo userInfo = userMapper.selectById(81);
        return userInfo.toString();
    }
    @GetMapping("/getlist")
    public String testgetlist(){
        List<UserInfo> userInfos = userMapper.selectList(null);
        return userInfos.toString();
    }

    @GetMapping("/webfluxtest")
    public Mono<String> helloword(){
        return Mono.just("Hello webFlux!").publishOn(Schedulers.single())
                .map(s->{
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){}
                    return "test1";
                });
    }

    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> flux() {
        Flux<String> flux = Flux.fromArray(new String[]{"a", "b", "c", "d"}).map(s -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "<letter:" + s + ">";
        });
        return flux;
    }
}
